package com.nuanshui.heatedloan.dal;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.Alias;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

@Alias("setTypeHandler")
public class SetTypeHandler extends BaseTypeHandler<HashSet<String>> {

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i,
			HashSet<String> parameter, JdbcType jdbcType) throws SQLException {		
	}

	@Override
	public HashSet<String> getNullableResult(ResultSet rs, String columnName)
			throws SQLException {
		return getSet(rs.getString(columnName));
	}

	@Override
	public HashSet<String> getNullableResult(ResultSet rs, int columnIndex)
			throws SQLException {
		 return getSet(rs.getString(columnIndex));
	}

	@Override
	public HashSet<String> getNullableResult(CallableStatement cs,
			int columnIndex) throws SQLException {
		return getSet(cs.getString(columnIndex));
	}
		
	private  HashSet<String> getSet(String str) {
		HashSet<String> set = new HashSet<String>();	
		if(StringUtils.isNotEmpty(str)) {
			String strs[]  = str.split(",");
			for(String s: strs){
				set.add(s);
			}
		}
		return set;
	}
	



}
